package com.xjc.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjc.business.entity.domain.SchoolAdminRelations;
import com.xjc.business.entity.domain.SchoolApplys;
import com.xjc.business.entity.domain.SchoolDetails;
import com.xjc.business.entity.domain.Schools;
import com.xjc.business.entity.po.schadmin.SchApplyPo;
import com.xjc.business.entity.po.sysadmin.ReplyApplyPo;
import com.xjc.business.entity.vo.PageVo;
import com.xjc.business.entity.vo.schadmin.SchDetailVo;
import com.xjc.business.entity.vo.sysadmin.SchoolApplyPageVo;
import com.xjc.business.service.*;
import com.xjc.business.mapper.SchoolApplysMapper;
import com.xjc.system.constants.MyConstants;
import com.xjc.system.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author root
* @description 针对表【school_applys(学校账户申请表)】的数据库操作Service实现
* @createDate 2025-01-25 10:24:32
*/
@Service
public class SchoolApplysServiceImpl extends ServiceImpl<SchoolApplysMapper, SchoolApplys>
    implements SchoolApplysService {

    @Autowired
    @Lazy
    SchoolAdminsService schoolAdminsService;

    @Autowired
    @Lazy
    SchoolsService schoolsService;

    @Autowired
    @Lazy
    SchoolDetailsService schoolDetailsService;


    @Autowired
    @Lazy
    SchoolAdminRelationsService schoolAdminRelationsService;

    /**
     * 分页查询学校申请
     *
     * @param pageSize    每页条数
     * @param currentPage 当前页码
     * @param schoolName  学校名称
     * @param status
     * @return 学校数据
     */
    @Override
    public Result<PageVo<SchoolApplyPageVo>> getSchoolApplyPage(Integer pageSize, Integer currentPage, String schoolName, Integer status) {
        LambdaQueryWrapper<SchoolApplys> schoolApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolApplysLambdaQueryWrapper.like(!schoolName.isEmpty(), SchoolApplys::getSchoolName, schoolName);
        schoolApplysLambdaQueryWrapper.eq(!status.equals(-1), SchoolApplys::getStatus, status);

        Page<SchoolApplys> schoolApplysPage = new Page<>(currentPage, pageSize);
        page(schoolApplysPage, schoolApplysLambdaQueryWrapper);
        List<SchoolApplys> schoolApplysPageRecords = schoolApplysPage.getRecords();

        List<SchoolApplyPageVo> schoolApplyPageVoList = schoolApplysPageRecords.stream().map(schoolApplys -> {
            SchoolApplyPageVo schoolApplyPageVo = new SchoolApplyPageVo();
            schoolApplyPageVo.setId(schoolApplys.getId());
            schoolApplyPageVo.setApplyUserName(schoolAdminsService.getById(schoolApplys.getApplyId()).getUsername());
            schoolApplyPageVo.setSchoolName(schoolApplys.getSchoolName());
            schoolApplyPageVo.setProvince(schoolApplys.getProvince());
            schoolApplyPageVo.setCity(schoolApplys.getCity());
            schoolApplyPageVo.setArea(schoolApplys.getArea());
            schoolApplyPageVo.setDetails(schoolApplys.getDetails());
            schoolApplyPageVo.setLevel(MyConstants.SCHOOL_LEVEL.get(schoolApplys.getLevel()));
            schoolApplyPageVo.setLevel2(MyConstants.SCHOOL_LEVEL2.get(schoolApplys.getLevel2()));
            schoolApplyPageVo.setPic(schoolApplys.getPic());
            schoolApplyPageVo.setCertificatePic(schoolApplys.getCertificatePic());
            schoolApplyPageVo.setPhone(schoolApplys.getPhone());
            schoolApplyPageVo.setEmail(schoolApplys.getEmail());
            schoolApplyPageVo.setStatus(MyConstants.SCHOOL_STATUS.get(schoolApplys.getStatus()));
            schoolApplyPageVo.setInfo(schoolApplys.getInfo());
            schoolApplyPageVo.setCreateTime(schoolApplys.getCreateTime());
            return schoolApplyPageVo;
        }).collect(Collectors.toList());

        PageVo<SchoolApplyPageVo> pageVo = new PageVo<>();
        pageVo.setData(schoolApplyPageVoList);
        pageVo.setTotal(schoolApplysPage.getTotal());
        pageVo.setCurrent(currentPage);
        pageVo.setSize(pageSize);
        pageVo.setPageCount(schoolApplysPage.getPages());

        return Result.okWithData(pageVo);
    }

    /**
     * 申请开通学校
     *
     * @param schApplyPo 学校信息
     * @return 反馈
     */
    @Override
    public Result<Integer> doApply(SchApplyPo schApplyPo) {
        SchoolApplys schoolApplys = new SchoolApplys();

        schoolApplys.setApplyId(schApplyPo.getApplyId());
        schoolApplys.setSchoolName(schApplyPo.getSchoolName());
        schoolApplys.setProvince(schApplyPo.getProvince());
        schoolApplys.setCity(schApplyPo.getCity());
        schoolApplys.setArea(schApplyPo.getArea());
        schoolApplys.setDetails(schApplyPo.getDetails());
        schoolApplys.setLevel(schApplyPo.getLevel());
        schoolApplys.setLevel2(schApplyPo.getLevel2());
        schoolApplys.setPic(schApplyPo.getPic());
        schoolApplys.setCertificatePic(schApplyPo.getCertificatePic());

        schoolApplys.setPhone(schApplyPo.getPhone());
        schoolApplys.setEmail(schApplyPo.getEmail());
        schoolApplys.setStatus(0);
        schoolApplys.setInfo("");
        schoolApplys.setCreateTime(new Date());
        schoolApplys.setUpdateTime(new Date());
        save(schoolApplys);

        return Result.okWithData(schoolApplys.getId());
    }

    /**
     * 获取申请中的学校详情
     * @param id 学校管理员id
     * @return 学校详情
     */
    @Override
    public Result<SchDetailVo> getApplySchoolInfo(Integer id) {
        LambdaQueryWrapper<SchoolApplys> schoolApplysLambdaQueryWrapper = new LambdaQueryWrapper<>();
        schoolApplysLambdaQueryWrapper.eq(SchoolApplys::getApplyId, id);
        SchoolApplys schoolApplysFromDB = getOne(schoolApplysLambdaQueryWrapper);

        SchDetailVo schDetailVo = new SchDetailVo();

        if ( schoolApplysFromDB!= null) {
            schDetailVo.setId(schoolApplysFromDB.getId());
            schDetailVo.setSchoolName(schoolApplysFromDB.getSchoolName());
            schDetailVo.setPic(schoolApplysFromDB.getPic());
            schDetailVo.setCertificatePic(schoolApplysFromDB.getCertificatePic());
            schDetailVo.setProvince(schoolApplysFromDB.getProvince());
            schDetailVo.setCity(schoolApplysFromDB.getCity());
            schDetailVo.setArea(schoolApplysFromDB.getArea());
            schDetailVo.setDetails(schoolApplysFromDB.getDetails());
            schDetailVo.setEmail(schoolApplysFromDB.getEmail());
            schDetailVo.setPhone(schoolApplysFromDB.getPhone());
            schDetailVo.setLevel((schoolApplysFromDB.getLevel()));
            schDetailVo.setLevel2((schoolApplysFromDB.getLevel2()));
            schDetailVo.setStatus(schoolApplysFromDB.getStatus());
        }
        return Result.okWithData(schDetailVo);
    }

    /**
     * 审核学校申请
     *
     * @param replyApplyPo 信息
     * @return 反馈
     */
    @Override
    @Transactional
    public Result<String> replyApply(ReplyApplyPo replyApplyPo) {
        Integer applyId = replyApplyPo.getApplyId();
        String info = replyApplyPo.getInfo();
        Integer status = replyApplyPo.getStatus();

        SchoolApplys schoolApplysFromDB  = getById(applyId);
        schoolApplysFromDB.setStatus(status);
        schoolApplysFromDB.setInfo(info);
        updateById(schoolApplysFromDB);

        // 通过
        if(status == 2){
            //创建学校信息
            Schools schools = new Schools();
            schools.setSchoolName(schoolApplysFromDB.getSchoolName());
            schools.setPic(schoolApplysFromDB.getPic());
            schools.setCreateTime(new Date());
            schools.setUpdateTime(new Date());
            schools.setCertificatePic(schoolApplysFromDB.getCertificatePic());
            schoolsService.save(schools);

            //创建学校详细信息
            SchoolDetails schoolDetails = new SchoolDetails();
            schoolDetails.setSchId(schools.getId());
            schoolDetails.setProvince(schoolApplysFromDB.getProvince());
            schoolDetails.setCity(schoolApplysFromDB.getCity());
            schoolDetails.setArea(schoolApplysFromDB.getArea());
            schoolDetails.setDetails(schoolApplysFromDB.getDetails());
            schoolDetails.setEmail(schoolApplysFromDB.getEmail());
            schoolDetails.setPhone(schoolApplysFromDB.getPhone());
            schoolDetails.setLevel(schoolApplysFromDB.getLevel());
            schoolDetails.setLevel2(schoolApplysFromDB.getLevel2());
            schoolDetails.setCreateTime(new Date());
            schoolDetails.setUpdateTime(new Date());

            schoolDetailsService.save(schoolDetails);

            //创建学校和管理员关系
            SchoolAdminRelations schoolAdminRelations = new SchoolAdminRelations();
            schoolAdminRelations.setSchId(schools.getId());
            schoolAdminRelations.setSchAdminId(schoolApplysFromDB.getApplyId());
            schoolAdminRelations.setCreateTime(new Date());
            schoolAdminRelations.setUpdateTime(new Date());

            schoolAdminRelationsService.save(schoolAdminRelations);
        }

        return Result.okWithData("审核成功");
    }
}




